package com.dayu.web.controller.recovery;

import java.util.List;

import com.dayu.common.annotation.RepeatSubmit;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.service.IRecoveryGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.recovery.domain.RecoveryStock;
import com.dayu.recovery.service.IRecoveryStockService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;

/**
 * 仓库管理Controller
 * 
 * @author HK
 * @date 2021-04-16
 */
@RestController
@RequestMapping("/recovery/stock")
public class RecoveryStockController extends BaseController {
    @Autowired
    private IRecoveryStockService recoveryStockService;
    @Autowired
    private IRecoveryGoodsService goodsService;

    /**
     * 查询仓库管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RecoveryStock recoveryStock) {
        startPage();
        List<RecoveryStock> list = recoveryStockService.selectRecoveryStockList(recoveryStock);
        return getDataTable(list);
    }

   /*根据权限获取列表*/
    @GetMapping("/list/power")
    public TableDataInfo listByPower(RecoveryStock recoveryStock)
    {
        startPage();
        List<RecoveryStock> list = recoveryStockService.selectRecoveryStockListByPower(recoveryStock);
        return getDataTable(list);
    }

    /**
     * 导出仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:stock:export')")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RecoveryStock recoveryStock)
    {
        List<RecoveryStock> list = recoveryStockService.selectRecoveryStockList(recoveryStock);
        ExcelUtil<RecoveryStock> util = new ExcelUtil<RecoveryStock>(RecoveryStock.class);
        return util.exportExcel(list, "stock");
    }

    /**
     * 获取仓库管理详细信息
     */
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") Long stockId)
    {
        return AjaxResult.success(recoveryStockService.selectRecoveryStockById(stockId));
    }

    /**
     * 新增仓库管理
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('recovery:stock:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecoveryStock recoveryStock)
    {
        return toAjax(recoveryStockService.insertRecoveryStock(recoveryStock));
    }

    /**
     * 修改仓库管理
     */
    @PreAuthorize("@ss.hasPermi('recovery:stock:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecoveryStock recoveryStock)
    {
        return toAjax(recoveryStockService.updateRecoveryStock(recoveryStock));
    }

    /**
     * 删除仓库管理
     */
    @PreAuthorize("@ss.hasPermi('recovery:stock:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable Long[] stockIds)
    {
        for(Long id : stockIds){
            RecoveryGoods goods = new RecoveryGoods();
            goods.setStockId(id);
            List<RecoveryGoods> goodsList = goodsService.selectRecoveryGoodsList(goods);
            if(goodsList != null && !goodsList.isEmpty()) {
                return AjaxResult.error("仓库下存在已售或待售商品数据，不可删除！");
            }
        }
        return toAjax(recoveryStockService.deleteRecoveryStockByIds(stockIds));
    }
}
